from sqlalchemy import Column, String, DateTime, Float, Integer, ForeignKey, DECIMAL
from sqlalchemy.orm import relationship
from datetime import datetime
from app.database import Base
import uuid

class AsphaltTest(Base):
    __tablename__ = "asphalt_test"

    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    equipment_number = Column(String(50), nullable=False, comment="设备编号")
    precative_number = Column(String(50), nullable=False, comment="预制编号")
    trial_date = Column(DateTime, nullable=False, comment="试验日期")
    created_at = Column(DateTime, default=datetime.utcnow, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment="更新时间")

    # 关联样本数据
    samples = relationship("AsphaltSample", back_populates="test", cascade="all, delete-orphan")

class AsphaltSample(Base):
    __tablename__ = "asphalt_sample"

    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    test_id = Column(String(36), ForeignKey("asphalt_test.id"), nullable=False, comment="关联的试验ID")
    sample_id = Column(String(20), nullable=False, comment="样本编号")
    compression_strength = Column(DECIMAL(10,2), nullable=False, comment="抗压强度(kyqd)")
    compression_load = Column(DECIMAL(10,2), nullable=False, comment="抗压荷载(kyhz)")
    created_at = Column(DateTime, default=datetime.utcnow, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment="更新时间")

    # 关联试验数据
    test = relationship("AsphaltTest", back_populates="samples")
    # 关联曲线数据
    curve_data = relationship("AsphaltCurveData", back_populates="sample", cascade="all, delete-orphan")

class AsphaltCurveData(Base):
    __tablename__ = "asphalt_curve_data"

    id = Column(Integer, primary_key=True, autoincrement=True)
    sample_id = Column(String(36), ForeignKey("asphalt_sample.id"), nullable=False, comment="关联的样本ID")
    x_value = Column(DECIMAL(20,10), nullable=False, comment="X轴数据")
    y_value = Column(DECIMAL(20,10), nullable=False, comment="Y轴数据")
    data_order = Column(Integer, nullable=False, comment="数据点顺序")
    created_at = Column(DateTime, default=datetime.utcnow, comment="创建时间")

    # 关联样本数据
    sample = relationship("AsphaltSample", back_populates="curve_data") 